﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 9/3/2009
 * Time: 1:10 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Buaya.Model;

namespace Buaya.Gui
{
	/// <summary>
	/// Description of TaxCodeForm.
	/// </summary>
	public partial class TaxCodeForm : BaseForm
	{
		public TaxCodeForm() : this(new TaxCode())
		{
			
		}
		
		public TaxCodeForm(TaxCode t)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			Model = t;
		}
		
		protected override void OnModelChanged(EventArgs e)
		{
			base.OnModelChanged(e);
			TaxCode t = Model as TaxCode;
			textBoxCode.Text = t.Code;
			textBoxDescription.Text = t.Description;
			textBoxPersonalExemption.Text = t.PersonalExemption.ToString("0.00");
			checkBoxInactive.Checked = t.Inactive;
			t.TaxRangeAdded += delegate { RefreshList(); };
			RefreshList();
		}
		
		void RefreshList()
		{
			listView1.Items.Clear();
			foreach (TaxSalaryRange r in (Model as TaxCode).Ranges) {
				ListViewItem li = listView1.Items.Add(r.MonthlyStartSalary.ToString("0.00"));
				li.SubItems.Add(r.MonthlyEndSalary.ToString("0.00"));
				li.SubItems.Add(r.MonthlyBaseTax.ToString("0.00"));
				li.SubItems.Add(r.SemiMonthlyStartSalary.ToString("0.00"));
				li.SubItems.Add(r.SemiMonthlyEndSalary.ToString("0.00"));
				li.SubItems.Add(r.SemiMonthlyBaseTax.ToString("0.00"));
				li.SubItems.Add(r.PercentOver.ToString("0.00"));
			}
		}
		
		void ButtonOkClick(object sender, EventArgs e)
		{
			TaxCode t = Model as TaxCode;
			t.Code = textBoxCode.Text;
			t.Description = textBoxDescription.Text;
			t.PersonalExemption = Util.ConvertUtility.ToDouble(textBoxPersonalExemption.Text);
			t.Inactive = checkBoxInactive.Checked;
			SessionFacade.SaveOrUpdateTaxCode(t);
			Close();
		}
		
		void ButtonCancelClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void ButtonNewClick(object sender, EventArgs e)
		{
			using (TaxSalaryRangeForm f = new TaxSalaryRangeForm()) {
				if (MainForm.Instance.AddDialog(f) == DialogResult.OK) {
					(Model as TaxCode).AddTaxRange(f.Model as TaxSalaryRange);
				}
			}
		}
		
		void ButtonEditClick(object sender, EventArgs e)
		{
			if (listView1.SelectedItems.Count > 0) {
				TaxSalaryRange r = (Model as TaxCode).Ranges[listView1.SelectedItems[0].Index];
				using (TaxSalaryRangeForm f = new TaxSalaryRangeForm(r)) {
					if (MainForm.Instance.AddDialog(f) == DialogResult.OK) {
						RefreshList();
					}
				}
			}
		}
		
		void ButtonDeleteClick(object sender, EventArgs e)
		{
			if (listView1.SelectedItems.Count > 0) {
				TaxSalaryRange r = (Model as TaxCode).Ranges[listView1.SelectedItems[0].Index];
				if (Util.MessageService.Show("Are you sure you want to delete?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) {
					(Model as TaxCode).Ranges.Remove(r);
					RefreshList();
				}
			}
		}
	}
}
